package cz.gillan.housecosts;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import cz.gillan.housecosts.data.Gauge;
import cz.gillan.housecosts.sql.GaugeDbAdapter;

public class GaugeEditActivity extends Activity {
	//private TextView gaugeTypeLabel;
	private Spinner gaugeTypeEdit;
	//private TextView gaugeNumberLabel;
	private EditText gaugeNumberEdit;
	private EditText gaugeNameEdit;
	//private TextView gaugeOpeningDateLabel;
	private EditText gaugeOpeningDateEdit;
//	private TextView gaugeOpeningStateLabel;
	private EditText gaugeOpeningStateEdit;
//	private TextView gaugeClosingDateLabel;
	private EditText gaugeClosingDateEdit;
//	private TextView gaugeClosingStateLabel;
	private EditText gaugeClosingStateEdit;
	private EditText gaugeCommentEdit;
	private Button confirmButton;
	
	private GaugeDbAdapter gaugeDbAdapter;
	private Long rowId = null;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	gaugeDbAdapter = new GaugeDbAdapter(this);
    	gaugeDbAdapter.open();
        setContentView(R.layout.gauge_edit);
        
        gaugeTypeEdit = (Spinner) findViewById(R.id.gaugeTypeEdit);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.gaugeTypesArray, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        gaugeTypeEdit.setAdapter(adapter);
        gaugeNameEdit = (EditText)findViewById(R.id.gaugeNameEdit);
        gaugeNumberEdit = (EditText)findViewById(R.id.gaugeNumberEdit);
        gaugeCommentEdit = (EditText)findViewById(R.id.gaugeCommentEdit);
        
        confirmButton = (Button)findViewById(R.id.gauge_edit_button);
        Bundle extras = getIntent().getExtras();
        rowId = (savedInstanceState == null) ? null : (Long) savedInstanceState.getSerializable(GaugeDbAdapter.KEY_ROWID);
		if (extras != null) {
			rowId = extras.getLong(GaugeDbAdapter.KEY_ROWID);
		}
		
		populateFields();
		
		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				setResult(RESULT_OK);
				finish();
			}

		});
    }
    
    
//    @Override
//    public void onBackPressed() {
//    	super.onBackPressed();
//    }
    
    @Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void populateFields() {
		if (rowId != null) {
			Cursor gaugeCursor = gaugeDbAdapter.fetchGauge(rowId);
			startManagingCursor(gaugeCursor);
			gaugeNameEdit.setText(gaugeCursor.getString(gaugeCursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_NAME)));
			gaugeNumberEdit.setText(gaugeCursor.getString(gaugeCursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_SERIALNUMBER)));
			gaugeCommentEdit.setText(gaugeCursor.getString(gaugeCursor.getColumnIndexOrThrow(GaugeDbAdapter.KEY_COMMENT)));
		}
	}
	
	private void saveState() {
		Gauge gauge = new Gauge();
		gauge.setName(gaugeNameEdit.getText().toString());
		gauge.setSerialNumber(gaugeNumberEdit.getText().toString());
		gauge.setComment(gaugeCommentEdit.getText().toString());

		if (rowId == null) {
			if (gaugeDbAdapter.createNewGauge(gauge)) {
				rowId = gauge.getId();
			}
		} else {
			gaugeDbAdapter.updateGauge(rowId, gauge);
		}
	}
}
